package org.jeecg.modules.system.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.extern.slf4j.Slf4j;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.system.query.QueryGenerator;
import org.jeecg.common.util.oConvertUtils;
import org.jeecg.modules.system.entity.CrawlWebsite;
import org.jeecg.modules.system.service.IScrawlWebsiteService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.io.IOException;
import java.util.Date;


/**
 * @Author wt
 * @Date: 2024/9/24 11:19
 * @Description:
 */

@RestController
@RequestMapping("/sys/CrawlWebsite")
@Slf4j
public class CrawlWebsiteController {
    @Autowired
    private IScrawlWebsiteService iScrawlWebsiteService;

    /**
     * 分页列表查询
     * @param crawlWebsite
     * @param pageNo
     * @param pageSize
     * @param req
     * @return
     */
    @RequestMapping(value = "/list", method = RequestMethod.GET)
    public Result<IPage<CrawlWebsite>> queryPageList(CrawlWebsite crawlWebsite,
            @RequestParam(name="pageNo", defaultValue="1") Integer pageNo,
            @RequestParam(name="pageSize", defaultValue="10") Integer pageSize, HttpServletRequest req) {
        Result<IPage<CrawlWebsite>> result = new Result<IPage<CrawlWebsite>>();
        QueryWrapper<CrawlWebsite> query = QueryGenerator.initQueryWrapper(crawlWebsite, req.getParameterMap());
        Page<CrawlWebsite> page = new Page<CrawlWebsite>(pageNo, pageSize);
        IPage<CrawlWebsite> pageList = iScrawlWebsiteService.page(page, query);
        result.setSuccess(true);
        result.setResult(pageList);
        log.info("=====result======"+result);
        return result;
    }

    /**
     *   新增
     * @param crawlWebsite
     * @return
     */
    @RequestMapping(value = "/add", method = RequestMethod.POST)
    public Result<CrawlWebsite> add(@RequestBody CrawlWebsite crawlWebsite) {
        Result<CrawlWebsite> result = new Result<CrawlWebsite>();
        try {
            crawlWebsite.setCreateTime(new Date());
            log.info("=====名称====="+crawlWebsite.getName());
            log.info("=====id====="+crawlWebsite.getId());
            iScrawlWebsiteService.insertCrawlWebsite(crawlWebsite);
            result.success("添加成功！");
        } catch (Exception e) {
            log.error(e.getMessage(), e);
            result.error500("操作失败");
        }
        return result;
    }

    /**
     *  编辑
     * @param crawlWebsite
     * @return
     */
    @RequestMapping(value = "/edit", method = RequestMethod.PUT)
    public Result<CrawlWebsite> edit(@RequestBody CrawlWebsite crawlWebsite) {
        Result<CrawlWebsite> result = new Result<CrawlWebsite>();
        CrawlWebsite crawlWebsite1 = iScrawlWebsiteService.getById(crawlWebsite.getId());
        if(crawlWebsite1==null) {
            result.error500("未找到对应实体");
        }else {
            try {
                boolean ok = iScrawlWebsiteService.updateById(crawlWebsite);
                if(ok) {
                    result.success("修改成功!");
                }
            } catch (Exception e) {
                log.error(e.getMessage(), e);
                result.error500("操作失败");
            }
        }
        return result;
    }

    /**
     *   通过id删除
     * @param id
     * @return
     */
    @RequestMapping(value = "/delete", method = RequestMethod.DELETE)
    public Result<?> delete(@RequestParam(name="id",required=true) Long id) throws IOException {
        iScrawlWebsiteService.deleteWebsite(id);
        return Result.ok("删除网站成功");
    }

    /**
     *  批量删除
     * @param ids
     * @return
     */
    @RequestMapping(value = "/deleteBatch", method = RequestMethod.DELETE)
    public Result<CrawlWebsite> deleteBatch(@RequestParam Long[] ids) {
        Result<CrawlWebsite> result = new Result<CrawlWebsite>();
        if(oConvertUtils.isEmpty(ids)) {
            result.error500("未选中网站！");
        }else {
            iScrawlWebsiteService.deleteBatchWebsite(ids);
            result.success("删除知网站成功!");
        }
        return result;
    }


}
